import UIKit
import BraintreeDropIn
import Braintree
class ViewController: UIViewController {
@IBOutlet weak var btnShow: UIButton!
@IBOutlet weak var indicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func fetchClientToken() {
self.btnShow.isEnabled = false
self.indicator.isHidden = false
// TODO: Switch this URL to your own authenticated API
// 測試用官方取得Token Url
let clientTokenURL = NSURL(string: "https://braintree-sample-merchant.herokuapp.com/client_token")!
let clientTokenRequest = NSMutableURLRequest(url: clientTokenURL as URL)
clientTokenRequest.setValue("text/plain", forHTTPHeaderField: "Accept")
URLSession.shared.dataTask(with: clientTokenRequest as URLRequest) { (data, response, error) -> Void in
// TODO: Handle errors
let clientToken = String(data: data!, encoding: String.Encoding.utf8)
print("clientToken:\(clientToken!)")
self.showDropIn(clientTokenOrTokenizationKey: clientToken!)
// As an example, you may wish to present Drop-in at this point.
// Continue to the next section to learn more...
}.resume()
}
@IBAction func jumpDropIn(_ sender: UIButton) {
fetchClientToken()
}
func showDropIn(clientTokenOrTokenizationKey: String) {
DispatchQueue.main.async {
let request = BTDropInRequest()
let dropIn = BTDropInController(authorization: clientTokenOrTokenizationKey, request: request) { (controller, result, error) in
if (error != nil) {
print("ERROR:\(error.debugDescription)")
} else if (result?.isCancelled == true) {
print("CANCELLED")
} else if let result = result {
// Use the BTDropInResult properties to update your UI
print("result paymentOptionType:\(result.paymentOptionType)")
// 取得nonce後,拿nonce去後端註冊
print("result paymentMethod.nonce:\(result.paymentMethod!.nonce)")
print("result paymentIcon:\(result.paymentIcon)")
print("result paymentDescription:\(result.paymentDescription)")
}
self.btnShow.isEnabled = true
self.indicator.isHidden = true
controller.dismiss(animated: true, completion: nil)
}
self.present(dropIn!, animated: true, completion: nil)
}
}
}
package com.terryyamg.realmtest;
import android.app.Application;
import io.realm.Realm;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Initialize Realm. Should only be done once when the application starts.
Realm.init(this);
}
}
package com.terryyamg.realmtest;
import io.realm.RealmList;
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
public class Person extends RealmObject{
@PrimaryKey
private int id;
private String name;
private int age;
private RealmList<Company> company;
public void setId(int id){
this.id = id;
}
public int getId(){
return id;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public void setCompany(RealmList<Company> company){
this.company = company;
}
public RealmList<Company> getCompany(){
return company;
}
}
7.Company.java
package com.terryyamg.realmtest;
import io.realm.RealmObject;
public class Company extends RealmObject{
private int id;
private String companyName;
public void setId(int id){
this.id = id;
}
public int getId(){
return id;
}
public void setCompanyName(String companyName){
this.companyName = companyName;
}
public String getCompanyName(){
return companyName;
}
}